package com.CampaignManager.client;

import java.util.Date;

import com.CampaignManager.shared.CityDTO;
import com.CampaignManager.shared.JobDTO;
import com.CampaignManager.shared.PlayerDTO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class EspionageForm extends Composite {

	private static EspionageFormUiBinder uiBinder = GWT
			.create(EspionageFormUiBinder.class);

	interface EspionageFormUiBinder extends UiBinder<Widget, EspionageForm> {
	}
	
	private CampaignManager mainForm = null;
	private String cityName = null;
	private JobDTO job = null;
	private PlayerDTO playerInfo = null;
	
	public EspionageForm(String cityName, CampaignManager mainForm, PlayerDTO playerInfo) {

		this.mainForm = mainForm;
		this.cityName = cityName;
		this.playerInfo = playerInfo;
		
		initWidget(uiBinder.createAndBindUi(this));
		
		resourceForm.RefreshTable(playerInfo);
		exploitForm.RefreshTable(cityName, playerInfo, mainForm, resourceForm);
		
		this.lblTitle.setText("Available Espionage Options in "+cityName);
		
		SetupTables();
		
		for (CityDTO city : mainForm.Cities) {
			if (!cityName.equals(city.getName())) {
				this.otherCity.addItem(city.getName(),city.getName());
			}
		}
	}
	
	@UiField
	Label lblTitle;
	
	@UiField
	ResourceForm resourceForm;
	@UiField
	SpendEspionageForm exploitForm;
	
	@UiField
	ListBox otherCity;	
	@UiField
	FlexTable spyTable;
	@UiField
	FlexTable warmongerTable;
	
	@UiField
	Label jobDetail;
	
	@UiField
	HTML detailsHtml;
	@UiField
	HTML unavailableHtml;
	
	private void SetupTables() {
		
		SetupSpyTable();
		SetupWarmongerTable();
	}
	private void SetupSpyTable() {
			
		int ongoingEspionage = playerInfo.getOngoingEspionage().size();
		
		spyTable.setText(0, 0, "Surveillance");
		spyTable.setText(0, 1, "Level 1");
		Button surveillance = new Button("Undertake");
		surveillance.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				SelectActivity("Surveillance",1L);				
			}
		});
		if (ongoingEspionage > 0) {
			surveillance.setEnabled(false);
			surveillance.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 0L) {
			surveillance.setEnabled(false);
			surveillance.setTitle("?");
		}
		spyTable.setWidget(0, 2, surveillance);
		
		spyTable.setText(1, 0, "Bribe a servant");
		spyTable.setText(1, 1, "Level 2");		
		Button bribeServant = new Button("Undertake");
		bribeServant.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				SelectActivity("Bribe",2L);
			}
		});	
		if (ongoingEspionage > 0) {
			bribeServant.setEnabled(false);
			bribeServant.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 1L) {
			bribeServant.setEnabled(false);
			bribeServant.setTitle("Requires Espionage level 1, you are level "+playerInfo.getEspionageLevel());
		}
		spyTable.setWidget(1, 2, bribeServant);
		
		spyTable.setText(2, 0, "Bribe an agent");
		spyTable.setText(2, 1, "Level 3");
		Button bribeAgent = new Button("Undertake");
		bribeAgent.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {	
				SelectActivity("Bribe",3L);		
			}
		});
		if (ongoingEspionage > 0) {
			bribeAgent.setEnabled(false);
			bribeAgent.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 2L) {
			bribeAgent.setEnabled(false);
			bribeAgent.setTitle("Requires Espionage level 2, you are level "+playerInfo.getEspionageLevel());
		}
		spyTable.setWidget(2, 2, bribeAgent);
		
		spyTable.setText(3, 0, "Bribe an advisor");
		spyTable.setText(3, 1, "Level 4");
		Button bribeAdvisor = new Button("Undertake");
		bribeAdvisor.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {	
				SelectActivity("Bribe",4L);
			}
		});
		if (ongoingEspionage > 0) {
			bribeAdvisor.setEnabled(false);
			bribeAdvisor.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 3L) {
			bribeAdvisor.setEnabled(false);
			bribeAdvisor.setTitle("Requires Espionage level 3, you are level "+playerInfo.getEspionageLevel());
		}
		spyTable.setWidget(3, 2, bribeAdvisor);
	}
	private void SetupWarmongerTable() {
		
		int ongoingEspionage = playerInfo.getOngoingEspionage().size();
		
		warmongerTable.setText(0, 0, "SpreadRumor");
		warmongerTable.setText(0, 1, "Level 1");
		Button SpreadRumor = new Button("Undertake");
		SpreadRumor.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {				
				SelectActivity("Warmonger",1L);		
			}
		});
		if (ongoingEspionage > 0) {
			SpreadRumor.setEnabled(false);
			SpreadRumor.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 1L) {
			SpreadRumor.setEnabled(false);
			SpreadRumor.setTitle("Requires Espionage level 1, you are level "+playerInfo.getEspionageLevel());
		}
		warmongerTable.setWidget(0, 2, SpreadRumor);
		
		warmongerTable.setText(1, 0, "Impersonate");
		warmongerTable.setText(1, 1, "Level 2");
		Button Impersonate = new Button("Undertake");
		Impersonate.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {				
				SelectActivity("Warmonger",2L);	
			}
		});
		if (ongoingEspionage > 0) {
			Impersonate.setEnabled(false);
			Impersonate.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 2L) {
			Impersonate.setEnabled(false);
			Impersonate.setTitle("Requires Espionage level 2, you are level "+playerInfo.getEspionageLevel());
		}
		warmongerTable.setWidget(1, 2, Impersonate);
		
		warmongerTable.setText(2, 0, "Man-in-the-middle");
		warmongerTable.setText(2, 1, "Level 3");
		Button maninmiddle = new Button("Undertake");
		maninmiddle.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {				
				SelectActivity("Warmonger",3L);					
			}
		});
		if (ongoingEspionage > 0) {
			maninmiddle.setEnabled(false);
			maninmiddle.setTitle("already undertaking espionage...");
		} else if (playerInfo.getEspionageLevel() < 3L) {
			maninmiddle.setEnabled(false);
			maninmiddle.setTitle("Requires Espionage level 3, you are level "+playerInfo.getEspionageLevel());
		}
		warmongerTable.setWidget(2, 2, maninmiddle);
	}

	private void SelectActivity(String jobType, Long level) {
		
		job = new JobDTO();
		job.setJobType(jobType);
		job.setJobFor(this.cityName);
		if (jobType.startsWith("Warmonger")) {
			String itemValue = otherCity.getValue(otherCity.getSelectedIndex());
			job.setDestination(itemValue);
		}
		job.setReward(level);
		
		jobDetail.setText(job.getJobDescription());
		
		mainForm.campaignService.CheckExistingPendingJob(new AsyncCallback<Boolean>() {
    		public void onFailure(Throwable error) {
    			mainForm.handleError(error);
			}
			public void onSuccess(Boolean result) {
				if (result) {
					String html = "<p>You already have a pending job.  You need to go to the <a href=https://groups.google.com/forum/#!newtopic/"+PlayerDTO.GROUP_NAME+" target=_blank>Group</a> and post a topic with the story for the last job before taking another one.</p>";
					unavailableHtml.setHTML(html);
					unavailableHtml.setVisible(true);
					detailsHtml.setVisible(false);
					btnFinish.setVisible(false);
				} else {
					if (Window.confirm("Undertaking espionage requires that you stay in the city until completion")) {
						mainForm.campaignService.UpdateArmyDestination(playerInfo.getEmailAddress(), playerInfo.getOriginLatitude(), playerInfo.getOriginLongitude(), playerInfo.getOriginLatitude(), playerInfo.getOriginLongitude(), 0L, new AsyncCallback<Void>() {
							public void onFailure(Throwable error) {
								mainForm.handleError(error);
			    			}
			    			public void onSuccess(Void ignore) {
			    				playerInfo.setDestLatitude(playerInfo.getOriginLatitude());
			    				playerInfo.setDestLongitude(playerInfo.getOriginLongitude());
			    				playerInfo.setMilesToTravel(0L);
			    				playerInfo.setMoveStart(new Date());

								unavailableHtml.setVisible(false);
								detailsHtml.setVisible(true);
								btnFinish.setVisible(true);
			    			}
						});
					}
				}
			}
		});
	}
	
	@UiField
	Button btnFinish;
	@UiHandler("btnFinish")
	void btnFinishClick(ClickEvent event) {
		this.spyTable.setVisible(false);
		this.btnFinish.setText("Creating Job Record...");
		this.btnFinish.setEnabled(false);
		
		mainForm.campaignService.CreateJob(job, new AsyncCallback<String>() {
    		public void onFailure(Throwable error) {
    			mainForm.handleError(error);
			}
			public void onSuccess(String result) {
				job.setId(result);
				job.setTaken(new Date());
				playerInfo.getJobs().add(job);
				mainForm.showActivities();
			}
		});
		
		Window.open("https://groups.google.com/forum/#!newtopic/"+PlayerDTO.GROUP_NAME, "_blank", "");
	}
}
